//
//  TransformationViewController.m
//  OpenGLES_Learning
//
//  Created by vandong on 8/29/13.
//  Copyright (c) 2013 Mac Center. All rights reserved.
//

#import "TransformationViewController.h"
#import "lowPolyAxesAndModels2.h"

@interface TransformationViewController ()

@end

@implementation TransformationViewController
@synthesize baseEffect;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    GLKView *glView = (GLKView *)self.view;
    NSAssert([glView isKindOfClass:[GLKView class]], @"Sorry you dont have GLKView");
    
    glView.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    glView.drawableDepthFormat = GLKViewDrawableDepthFormat16;
    [EAGLContext setCurrentContext:glView.context];
    
    self.baseEffect = [[GLKBaseEffect alloc] init];
    self.baseEffect.useConstantColor = GL_TRUE;
    self.baseEffect.constantColor = GLKVector4Make(1.0, 1.0, 1.0, 1.0);
    glClearColor(0.0, 0.0, 0.0, 1.0);
    
    self.baseEffect.light0.enabled = GL_TRUE;
    self.baseEffect.light0.ambientColor = GLKVector4Make(0.4, 0.4, 0.4, 1.0);
    self.baseEffect.light0.position = GLKVector4Make(1.0, 0.8, 0.4, 0.0);
    self.baseEffect.light0.diffuseColor = GLKVector4Make(
                                                         1.0f, // Red
                                                         1.0f, // Green
                                                         1.0f, // Blue 
                                                         1.0f);// Alpha
    
    glGenBuffers(1, &vertexBufferId);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(lowPolyAxesAndModels2Verts), lowPolyAxesAndModels2Verts, GL_STATIC_DRAW);

    glGenBuffers(1, &normalBufferId);
    glBindBuffer(GL_ARRAY_BUFFER, normalBufferId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(lowPolyAxesAndModels2Normals), lowPolyAxesAndModels2Normals, GL_STATIC_DRAW);
    
    glEnable(GL_DEPTH_TEST);

    GLKMatrix4 modelviewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(30.0f), 1.0, 0.0, 0.0);
    modelviewMatrix = GLKMatrix4Rotate(modelviewMatrix, GLKMathDegreesToRadians(-30.0f), 0.0, 1.0, 0.0);
    modelviewMatrix = GLKMatrix4Translate(modelviewMatrix, -0.25, 0.0, -0.20);
    self.baseEffect.transform.modelviewMatrix = modelviewMatrix;
    curModelviewMatrix = modelviewMatrix;
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureHandler:)];
    [self.view addGestureRecognizer:panGesture];
}

- (void)viewDidUnload {
    GLKView *v = (GLKView *)self.view;
    [EAGLContext setCurrentContext:v.context];
    
    if (vertexBufferId != 0) {
        glDeleteBuffers(1, &vertexBufferId);
        vertexBufferId = 0;
    }
    if (normalBufferId != 0) {
        glDeleteBuffers(1, &normalBufferId);
        normalBufferId = 0;
    }
    
    v.context = nil;
    [EAGLContext setCurrentContext:nil];
}

- (void)dealloc {
    GLKView *v = (GLKView *)self.view;
    [EAGLContext setCurrentContext:v.context];
    
    if (vertexBufferId != 0) {
        glDeleteBuffers(1, &vertexBufferId);
        vertexBufferId = 0;
    }
    if (normalBufferId != 0) {
        glDeleteBuffers(1, &normalBufferId);
        normalBufferId = 0;
    }
    
    v.context = nil;
    [EAGLContext setCurrentContext:nil];

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
    const GLfloat  aspectRatio = (GLfloat)view.drawableWidth / (GLfloat)view.drawableHeight;
    
    self.baseEffect.transform.projectionMatrix =
    GLKMatrix4MakeOrtho(
                        -0.5 * aspectRatio,
                        0.5 * aspectRatio,
                        -0.5,
                        0.5, 
                        -5.0, 
                        5.0);
    
    [self.baseEffect prepareToDraw];
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), NULL);
    
    glBindBuffer(GL_ARRAY_BUFFER, normalBufferId);
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), NULL);
    
    glDrawArrays(GL_TRIANGLES, 0, lowPolyAxesAndModels2NumVerts);
}


- (void)panGestureHandler:(UIPanGestureRecognizer *)gesture {
    
}

@end
